Skip to content

Fix: URLSanitizer does not validate/limit URL ports and can permit access to sensitive ports - #1661

Merged
steam-bell-92 merged 1 commit into
steam-bell-92:mainfrom
Kirtan-pc:fix/no-validation
Jul 12, 2026
Merged

Fix: URLSanitizer does not validate/limit URL ports and can permit access to sensitive ports#1661
steam-bell-92 merged 1 commit into
steam-bell-92:mainfrom
Kirtan-pc:fix/no-validation

Conversation

@Kirtan-pc

@Kirtan-pc Kirtan-pc commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

📝 Description

Summary

Add port validation to URLSanitizer.validate_url() to prevent SSRF attacks targeting dangerous ports (e.g., SSH:22, MySQL:3306, Redis:6379, Docker:2375) on allowed hosts.

Changes

security/url_sanitizer.py

  • BLOCKED_PORTS: Added class constant with ~80 known dangerous ports spanning SSH, SMB, MySQL, PostgreSQL, Redis, Docker, Memcached, MongoDB, LDAP, SMTP, and more.
  • allowed_ports parameter: Added to __init__ — when provided, acts as an allowlist (only these ports are permitted); when None, the BLOCKED_PORTS deny list is used.
  • _validate_port(): New method called during validate_url() that checks the parsed port against the blocked set or allowlist. Also validates port range (1–65535).
  • Convenience validate_url(): Updated to accept and forward allowed_ports.

tests/test_security.py

Added 5 new tests:

  • test_blocked_port — blocked port (SSH:22) on a public hostname is rejected
  • test_blocked_port_on_ip_literal — blocked port on an IP literal is rejected
  • test_allowed_ports_allowlist — allowlist mode permits specified ports
  • test_allowed_ports_rejects_non_allowed — allowlist rejects unspecified ports
  • test_valid_port_not_blocked — non-blocked port passes validation

🔗 Linked Issue

Closes #1542


📋 Contribution Checklist

  • I have verified that my files are placed in the correct directory.
  • I have tested my changes thoroughly on my local machine.
  • GSSoC 2026: I have been formally assigned to this issue and noted it above.

@vercel

vercel Bot commented Jul 12, 2026

Copy link
Copy Markdown

@Kirtan-pc is attempting to deploy a commit to the Anuj's projects Team on Vercel.

A member of the Team first needs to authorize it.

@steam-bell-92 steam-bell-92 added type:bug Something isn't working level:beginner gssoc:approved GSSoC 2026 approving tag labels Jul 12, 2026
@steam-bell-92
steam-bell-92 merged commit 5f371d0 into steam-bell-92:main Jul 12, 2026
3 of 4 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🎉 Thank you for your contribution!

Your Pull Request has been merged successfully.
Thanks again for your support! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved GSSoC 2026 approving tag level:beginner type:bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: URLSanitizer does not validate/limit URL ports and can permit access to sensitive ports

2 participants